//引入必要模块
const express = require('express');
const mysql = require('../mysql');

//建立路由组
const router = express.Router();

//列表
router.get('/list',(req,res)=>{
    mysql.query('select * from xz_user',(err,result)=>{
        if(err) throw err;
        if(result != 0){
            return res.send(result);
        }
        res.send('0');
    });
});

//get登录
router.get('/get_login/:uname&:upwd',(req,res)=>{
    let body = req.params;

    mysql.query('select * from xz_user where uname=? and upwd=?',[body.uname,body.upwd],(err,result)=>{
        if(err) throw err;
        if(result.length != 0){
            return res.send('1');
        }
        res.send('0');
    });
});

//post登录
router.post('/post_login',(req,res)=>{
    let body = req.body;
    // console.log(body);
    mysql.query('select * from xz_user where uname=? and upwd=?',[body.uname,body.upwd],(err,result)=>{
        if(err) throw err;
        if(result.length != 0){
            return res.send('1');
        }
        res.send('0');
    });
});

//put登录
router.put('/put_login',(req,res)=>{
    let body = req.body;

    mysql.query('select * from xz_user where uname=? and upwd=?',[body.uname,body.upwd],(err,result)=>{
        if(err) throw err;
        if(result.length != 0){
            return res.send('1');
        }
        res.send('0');
    });
});

//用户删除
router.delete('/delete/:uid',(req,res)=>{
    let id = req.params.uid;

    mysql.query('delete from xz_user where uid=?',[id],(err,result)=>{
        if(err) throw err;
        res.send(result.affectedRows.toString());
    });
});

// //登录
// router.get('/login',(req,res)=>{
//     let body = req.query;
//     //console.log(body);
//     //判断是否为空
//     if(!body.uname){
//         return res.send('0');
//     }
//     if(!body.upwd){
//         return res.send('0');
//     }

//     //sql命令
//     mysql.query('select * from xz_user where uname=? and upwd=?',[body.uname,body.upwd],(err,result)=>{
//         if(err) throw err;
//         if(result.length != 0){
//             return res.send('1');
//         }
//         res.send('0');
//     });
// });

// //用户列表
// router.get('/list',(req,res)=>{
//     mysql.query('select * from xz_user',(err,result)=>{
//         if(err) throw err;
//         if(result.length != 0){
//             return res.send(result);
//         }
//         res.send('0');
//     });
// });

// //restful登录
// router.get('/restful/:uname&:upwd',(req,res)=>{
//     //获取数据
//     let body = req.params;
	
// 	//sql命令
// 	mysql.query('select * from xz_user where uname=? and upwd=?',[body.uname,body.upwd],(err,result)=>{
// 	    if(err) throw err;
// 	    if(result.length != 0){
// 	        return res.send('1');
// 	    }
// 	    res.send('0');
// 	});
//     //res.send(body.uname+'--'+body.upwd);
// });

// //restful删除
// router.delete('/delete_user/:uid',(req,res)=>{
//     let id = req.params.uid;

//     mysql.query('delete from xz_user where uid=?',[id],(err,result)=>{
//         if(err) throw err;
//         res.send(result.affectedRows.toString());
//     });
// });

// //restful post 登录
// router.post('/post_login',(req,res)=>{
//     let body = req.body;

//     mysql.query('select * from xz_user where uname=? and upwd=?',[body.uname,body.upwd],(err,result)=>{
//         if(err) throw err;
//         if(result.length != 0){
//             return res.send('1');
//         }
//         res.send('0');
//     });

// });

// //restful put 登录
// router.put('/put_login',(req,res)=>{
//     let body = req.body;

//     mysql.query('select * from xz_user where uname=? and upwd=?',[body.uname,body.upwd],(err,result)=>{
//         if(err) throw err;
//         if(result.length != 0){
//             return res.send('1');
//         }
//         res.send('0');
//     });

// });

//导出对象
module.exports = router;